/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * okienko.java
 *
 * Created on 2011-01-18, 20:02:01
 */

package testy;

import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 * Klasa z oknem programu
 * @author Michał Rudowicz
 */
public class okienko extends javax.swing.JFrame {
    public Quiz quiz;
    private Pytanie pyt;
    private boolean moznaNastepna = true;

    /**
     * Tworzy nowe okienko
     * @param quiz Quiz, który chcemy przeprowadzić
     */
    public okienko(Settings ust) {
        initComponents();
        try {
            this.quiz = new Quiz(ust.file, ust.poczatkowo, ust.karniakow);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getLocalizedMessage(), "Błąd", JOptionPane.ERROR_MESSAGE);
            System.exit(-1);
        }
        this.jProgressBar1.setMaximum(this.quiz.poczatkowoPytan);
        this.jSpinner1.setValue(quiz.kara);
    }

    /**
     * Funkcja losująca następne pytanie i wyświetlająca wszystko jak trzeba
     * w okienku.
     *
     * @param pierwsze Jeśli True, to nie sprawdza, czy podano jakąś odpowiedź.
     */
    private void losujPytanie(boolean  pierwsze) {
        // na pewno da się to zrobić ładniej
        if (!moznaNastepna){
            JOptionPane.showMessageDialog(rootPane, "Nie tak szybko, najpierw sprawdź tą odpowiedź!",
                    "eat your meal before you get the cookie", JOptionPane.ERROR_MESSAGE);
            return;
        }
        try {
            pyt = this.quiz.losujPytanie();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane,
                    ex.getMessage() + "\nProgram zakończy pracę.",
                    "Informacja", JOptionPane.INFORMATION_MESSAGE);
            System.exit(0);
        }
        this.jTextArea1.setText(pyt.pytanie);
        this.jRadioButton1.setText("(1) " + pyt.odpowiedzi.get(0).tresc);
        this.jRadioButton2.setText("(2) " + pyt.odpowiedzi.get(1).tresc);
        this.jRadioButton3.setText("(3) " + pyt.odpowiedzi.get(2).tresc);

        this.powtorzenLabel.setText(Integer.toString(pyt.count));
        this.jProgressBar1.setValue(this.quiz.pytania.size());
        this.SprawdzBtn.setEnabled(true);
        this.poprawnaLabel.setVisible(false);
        this.jRadioButton1.setBackground(this.getBackground());
        this.jRadioButton2.setBackground(this.getBackground());
        this.jRadioButton3.setBackground(this.getBackground());
        this.buttonGroup1.clearSelection();
        this.moznaNastepna = false;
    }

    /**
     * Funkcja sprawdzająca, która odpowiedź jest zaznaczona, i czyniąca
     * odpowiednie kroki w zależności od tego, czy odpowiedź jest
     * poprawna czy nie.
     */
    private void sprawdzPytanie() {
        int ans, nans;
        // na pewno da się to zrobić ładniej
        if (this.jRadioButton1.getSelectedObjects() != null) {
            ans = 0;
        } else if (this.jRadioButton2.getSelectedObjects() != null) {
            ans = 1;
        } else if (this.jRadioButton3.getSelectedObjects() != null){
            ans = 2;
        } else {
            JOptionPane.showMessageDialog(rootPane, "Musisz wybrać jakąś odpowiedź!", "Wybierz odpowiedź", JOptionPane.ERROR_MESSAGE);
            return;
        }
        this.SprawdzBtn.setEnabled(false);
        this.quiz.kara = (Integer) this.jSpinner1.getValue();
        nans = this.quiz.sprawdzPytanie(ans);
        if (nans < 0) {
            // poprawna odpowiedź!
            this.poprawnaLabel.setVisible(true);
        } else {
            // błędna odpowiedź
            switch (nans) {
                case 0:
                    this.jRadioButton1.setBackground(Color.red);
                    break;
                case 1:
                    this.jRadioButton2.setBackground(Color.red);
                    break;
                case 2:
                    this.jRadioButton3.setBackground(Color.red);
                    break;
                default:
                    System.err.println("Dziwne pytanie jest poprawne! " + nans);
            }
        }
        this.powtorzenLabel.setText(Integer.toString(pyt.count));
        this.moznaNastepna = true;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        SprawdzBtn = new javax.swing.JButton();
        NextBtn = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        poprawnaLabel = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        powtorzenLabel = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jProgressBar1 = new javax.swing.JProgressBar();
        jPanel5 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jSpinner1 = new javax.swing.JSpinner();
        jPanel6 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("no cześć");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                losujPytanie(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setLineWrap(true);
        jTextArea1.setRows(3);
        jTextArea1.setWrapStyleWord(true);
        jScrollPane1.setViewportView(jTextArea1);

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setMnemonic(java.awt.event.KeyEvent.VK_1);
        jRadioButton1.setText("jRadioButton1");

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setMnemonic(java.awt.event.KeyEvent.VK_2);
        jRadioButton2.setText("jRadioButton2");

        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setMnemonic(java.awt.event.KeyEvent.VK_3);
        jRadioButton3.setText("jRadioButton3");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 744, Short.MAX_VALUE)
                    .addComponent(jRadioButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 744, Short.MAX_VALUE)
                    .addComponent(jRadioButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 744, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jRadioButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton3)
                .addGap(35, 35, 35))
        );

        jScrollPane2.setViewportView(jPanel1);

        SprawdzBtn.setMnemonic('s');
        SprawdzBtn.setText("Sprawdź");
        SprawdzBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sprawdzPytanie(evt);
            }
        });

        NextBtn.setMnemonic('a');
        NextBtn.setText("Następne pytanie");
        NextBtn.setToolTipText("");
        NextBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nastepnePytanieBtn(evt);
            }
        });

        jButton1.setMnemonic('q');
        jButton1.setText("Wyjdź");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                WyjdzBtn(evt);
            }
        });

        poprawnaLabel.setForeground(new java.awt.Color(0, 153, 0));
        poprawnaLabel.setText("POPRAWNA ODPOWIEDŹ!");

        jTabbedPane1.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Dla tego pytania"));

        jLabel2.setText("Powtórzeń:");

        powtorzenLabel.setText("jLabel3");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 161, Short.MAX_VALUE)
                .addComponent(powtorzenLabel)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(powtorzenLabel))
                .addContainerGap(36, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Ogółem"));

        jLabel1.setText("Pozostało pytań");

        jProgressBar1.setValue(100);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addContainerGap(36, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Statystyki", jPanel2);

        jLabel3.setText("O ile zwiększać ilość powtórzeń w razie błędu:");

        jSpinner1.setModel(new javax.swing.SpinnerNumberModel(5, 0, 100, 1));

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 347, Short.MAX_VALUE)
                .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(68, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Konfiguracja", jPanel5);

        jLabel4.setText("testy");

        jLabel6.setText("#infeka2008 @ freenode");

        jLabel5.setText("http://code.google.com/p/flegtesty/");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5))
                .addContainerGap(497, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel5)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("O programie", jPanel6);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 763, Short.MAX_VALUE)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 763, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 763, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(SprawdzBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(NextBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 235, Short.MAX_VALUE)
                        .addComponent(poprawnaLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SprawdzBtn)
                    .addComponent(NextBtn)
                    .addComponent(jButton1)
                    .addComponent(poprawnaLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void losujPytanie(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_losujPytanie
        losujPytanie(true);
    }//GEN-LAST:event_losujPytanie

    private void WyjdzBtn(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_WyjdzBtn
        System.exit(0);
    }//GEN-LAST:event_WyjdzBtn

    private void nastepnePytanieBtn(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nastepnePytanieBtn
        losujPytanie(false);
    }//GEN-LAST:event_nastepnePytanieBtn

    private void sprawdzPytanie(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sprawdzPytanie
        if (((JButton) evt.getSource()).isEnabled())
            sprawdzPytanie();
    }//GEN-LAST:event_sprawdzPytanie

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                System.err.println("nie uruchamiaj tego tak");
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton NextBtn;
    private javax.swing.JButton SprawdzBtn;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel poprawnaLabel;
    private javax.swing.JLabel powtorzenLabel;
    // End of variables declaration//GEN-END:variables


}
